//
//  BirthdayDetailViewController.m
//  CangLongWnnianli
//
//  Created by 暗流澎涌 on 2025/1/27.
//

#import "BirthdayDetailViewController.h"

@interface BirthdayDetailViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *birthdayCardView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *lunarDateLabel;
@property (nonatomic, strong) UILabel *countdownLabel;
@property (nonatomic, strong) UIView *infoSectionView;
@property (nonatomic, strong) UIView *notificationSectionView;
@property (nonatomic, strong) UISwitch *notifyOnDaySwitch;
@property (nonatomic, strong) UISwitch *notifyOneDayBeforeSwitch;
@property (nonatomic, strong) UISwitch *notifyOneWeekBeforeSwitch;

@end

@implementation BirthdayDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self setupConstraints];
    [self updateUI];
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.0];
    self.title = @"生日详情";
    
    // 导航栏按钮
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" 
                                                                    style:UIBarButtonItemStylePlain 
                                                                   target:self 
                                                                   action:@selector(backButtonTapped)];
    self.navigationItem.leftBarButtonItem = backButton;
    
    UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" 
                                                                    style:UIBarButtonItemStylePlain 
                                                                   target:self 
                                                                   action:@selector(editButtonTapped)];
    self.navigationItem.rightBarButtonItem = editButton;
    
    // 创建滚动视图
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.scrollView];
    
    self.contentView = [[UIView alloc] init];
    [self.scrollView addSubview:self.contentView];
    
    // 创建生日卡片
    [self createBirthdayCard];
    
    // 创建信息区域
    [self createInfoSection];
    
    // 创建提醒设置区域
    [self createNotificationSection];
    
    // 创建删除按钮
    [self createDeleteButton];
}

- (void)createBirthdayCard {
    self.birthdayCardView = [[UIView alloc] init];
    self.birthdayCardView.layer.cornerRadius = 16;
    self.birthdayCardView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.birthdayCardView.layer.shadowOffset = CGSizeMake(0, 4);
    self.birthdayCardView.layer.shadowOpacity = 0.2;
    self.birthdayCardView.layer.shadowRadius = 12;
    [self.contentView addSubview:self.birthdayCardView];
    
    // 添加渐变背景
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[
        (id)[UIColor colorWithRed:0.4 green:0.6 blue:1.0 alpha:1.0].CGColor,
        (id)[UIColor colorWithRed:0.6 green:0.4 blue:1.0 alpha:1.0].CGColor
    ];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.birthdayCardView.layer insertSublayer:gradientLayer atIndex:0];
    
    // 表情图标
    UILabel *emojiLabel = [[UILabel alloc] init];
    emojiLabel.text = @"🎂";
    emojiLabel.font = [UIFont systemFontOfSize:48];
    emojiLabel.textAlignment = NSTextAlignmentCenter;
    [self.birthdayCardView addSubview:emojiLabel];
    
    // 姓名标签
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont boldSystemFontOfSize:24];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.birthdayCardView addSubview:self.nameLabel];
    
    // 农历日期标签
    self.lunarDateLabel = [[UILabel alloc] init];
    self.lunarDateLabel.font = [UIFont systemFontOfSize:18];
    self.lunarDateLabel.textColor = [UIColor whiteColor];
    self.lunarDateLabel.alpha = 0.9;
    self.lunarDateLabel.textAlignment = NSTextAlignmentCenter;
    [self.birthdayCardView addSubview:self.lunarDateLabel];
    
    // 倒计时标签
    self.countdownLabel = [[UILabel alloc] init];
    self.countdownLabel.font = [UIFont boldSystemFontOfSize:20];
    self.countdownLabel.textColor = [UIColor whiteColor];
    self.countdownLabel.textAlignment = NSTextAlignmentCenter;
    [self.birthdayCardView addSubview:self.countdownLabel];
    
    // 设置约束
    emojiLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.lunarDateLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.countdownLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [emojiLabel.topAnchor constraintEqualToAnchor:self.birthdayCardView.topAnchor constant:30],
        [emojiLabel.centerXAnchor constraintEqualToAnchor:self.birthdayCardView.centerXAnchor],
        
        [self.nameLabel.topAnchor constraintEqualToAnchor:emojiLabel.bottomAnchor constant:16],
        [self.nameLabel.leadingAnchor constraintEqualToAnchor:self.birthdayCardView.leadingAnchor constant:20],
        [self.nameLabel.trailingAnchor constraintEqualToAnchor:self.birthdayCardView.trailingAnchor constant:-20],
        
        [self.lunarDateLabel.topAnchor constraintEqualToAnchor:self.nameLabel.bottomAnchor constant:8],
        [self.lunarDateLabel.leadingAnchor constraintEqualToAnchor:self.birthdayCardView.leadingAnchor constant:20],
        [self.lunarDateLabel.trailingAnchor constraintEqualToAnchor:self.birthdayCardView.trailingAnchor constant:-20],
        
        [self.countdownLabel.topAnchor constraintEqualToAnchor:self.lunarDateLabel.bottomAnchor constant:16],
        [self.countdownLabel.leadingAnchor constraintEqualToAnchor:self.birthdayCardView.leadingAnchor constant:20],
        [self.countdownLabel.trailingAnchor constraintEqualToAnchor:self.birthdayCardView.trailingAnchor constant:-20],
        [self.countdownLabel.bottomAnchor constraintEqualToAnchor:self.birthdayCardView.bottomAnchor constant:-30]
    ]];
}

- (void)createInfoSection {
    self.infoSectionView = [self createSectionViewWithTitle:@"基本信息"];
    
    NSArray *infoItems = @[
        @{@"title": @"姓名", @"value": self.birthday.name},
        @{@"title": @"关系", @"value": self.birthday.relationship},
        @{@"title": @"农历生日", @"value": [NSString stringWithFormat:@"%@%@", [self.birthday lunarMonthName], [self.birthday lunarDayName]]},
        @{@"title": @"公历生日", @"value": [self formatDate:[self.birthday solarBirthdayDate]]}
    ];
    
    UIView *previousView = nil;
    for (NSDictionary *item in infoItems) {
        UIView *rowView = [self createInfoRowWithTitle:item[@"title"] value:item[@"value"]];
        [self.infoSectionView addSubview:rowView];
        
        rowView.translatesAutoresizingMaskIntoConstraints = NO;
        
        if (previousView) {
            [NSLayoutConstraint activateConstraints:@[
                [rowView.topAnchor constraintEqualToAnchor:previousView.bottomAnchor],
                [rowView.leadingAnchor constraintEqualToAnchor:self.infoSectionView.leadingAnchor],
                [rowView.trailingAnchor constraintEqualToAnchor:self.infoSectionView.trailingAnchor],
                [rowView.heightAnchor constraintEqualToConstant:50]
            ]];
        } else {
            [NSLayoutConstraint activateConstraints:@[
                [rowView.topAnchor constraintEqualToAnchor:self.infoSectionView.topAnchor constant:50],
                [rowView.leadingAnchor constraintEqualToAnchor:self.infoSectionView.leadingAnchor],
                [rowView.trailingAnchor constraintEqualToAnchor:self.infoSectionView.trailingAnchor],
                [rowView.heightAnchor constraintEqualToConstant:50]
            ]];
        }
        
        previousView = rowView;
    }
}

- (void)createNotificationSection {
    self.notificationSectionView = [self createSectionViewWithTitle:@"提醒设置"];
    
    // 生日当天提醒
    UIView *onDayView = [self createSwitchRowWithTitle:@"生日当天" subtitle:@"00:00 提醒"];
    self.notifyOnDaySwitch = [onDayView viewWithTag:100];
    [self.notificationSectionView addSubview:onDayView];
    
    // 提前一天提醒
    UIView *oneDayView = [self createSwitchRowWithTitle:@"提前一天" subtitle:@"20:00 提醒"];
    self.notifyOneDayBeforeSwitch = [oneDayView viewWithTag:100];
    [self.notificationSectionView addSubview:oneDayView];
    
    // 提前一周提醒
    UIView *oneWeekView = [self createSwitchRowWithTitle:@"提前一周" subtitle:@"20:00 提醒"];
    self.notifyOneWeekBeforeSwitch = [oneWeekView viewWithTag:100];
    [self.notificationSectionView addSubview:oneWeekView];
    
    // 设置约束
    onDayView.translatesAutoresizingMaskIntoConstraints = NO;
    oneDayView.translatesAutoresizingMaskIntoConstraints = NO;
    oneWeekView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [onDayView.topAnchor constraintEqualToAnchor:self.notificationSectionView.topAnchor constant:50],
        [onDayView.leadingAnchor constraintEqualToAnchor:self.notificationSectionView.leadingAnchor],
        [onDayView.trailingAnchor constraintEqualToAnchor:self.notificationSectionView.trailingAnchor],
        [onDayView.heightAnchor constraintEqualToConstant:60],
        
        [oneDayView.topAnchor constraintEqualToAnchor:onDayView.bottomAnchor],
        [oneDayView.leadingAnchor constraintEqualToAnchor:self.notificationSectionView.leadingAnchor],
        [oneDayView.trailingAnchor constraintEqualToAnchor:self.notificationSectionView.trailingAnchor],
        [oneDayView.heightAnchor constraintEqualToConstant:60],
        
        [oneWeekView.topAnchor constraintEqualToAnchor:oneDayView.bottomAnchor],
        [oneWeekView.leadingAnchor constraintEqualToAnchor:self.notificationSectionView.leadingAnchor],
        [oneWeekView.trailingAnchor constraintEqualToAnchor:self.notificationSectionView.trailingAnchor],
        [oneWeekView.heightAnchor constraintEqualToConstant:60]
    ]];
}

- (void)createDeleteButton {
    UIButton *deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [deleteButton setTitle:@"删除提醒" forState:UIControlStateNormal];
    [deleteButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    deleteButton.backgroundColor = [UIColor colorWithRed:0.9 green:0.3 blue:0.3 alpha:1.0];
    deleteButton.layer.cornerRadius = 12;
    deleteButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [deleteButton addTarget:self action:@selector(deleteButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:deleteButton];
    
    deleteButton.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [deleteButton.topAnchor constraintEqualToAnchor:self.notificationSectionView.bottomAnchor constant:30],
        [deleteButton.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:20],
        [deleteButton.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-20],
        [deleteButton.heightAnchor constraintEqualToConstant:50],
        [deleteButton.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-20]
    ]];
}

- (UIView *)createSectionViewWithTitle:(NSString *)title {
    UIView *sectionView = [[UIView alloc] init];
    sectionView.backgroundColor = [UIColor whiteColor];
    sectionView.layer.cornerRadius = 12;
    sectionView.layer.shadowColor = [UIColor blackColor].CGColor;
    sectionView.layer.shadowOffset = CGSizeMake(0, 2);
    sectionView.layer.shadowOpacity = 0.1;
    sectionView.layer.shadowRadius = 8;
    [self.contentView addSubview:sectionView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    [sectionView addSubview:titleLabel];
    
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [titleLabel.topAnchor constraintEqualToAnchor:sectionView.topAnchor constant:20],
        [titleLabel.leadingAnchor constraintEqualToAnchor:sectionView.leadingAnchor constant:20]
    ]];
    
    return sectionView;
}

- (UIView *)createInfoRowWithTitle:(NSString *)title value:(NSString *)value {
    UIView *rowView = [[UIView alloc] init];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [rowView addSubview:titleLabel];
    
    UILabel *valueLabel = [[UILabel alloc] init];
    valueLabel.text = value;
    valueLabel.font = [UIFont systemFontOfSize:16];
    valueLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    valueLabel.textAlignment = NSTextAlignmentRight;
    [rowView addSubview:valueLabel];
    
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    valueLabel.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [titleLabel.leadingAnchor constraintEqualToAnchor:rowView.leadingAnchor constant:20],
        [titleLabel.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor],
        
        [valueLabel.trailingAnchor constraintEqualToAnchor:rowView.trailingAnchor constant:-20],
        [valueLabel.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor],
        [valueLabel.leadingAnchor constraintEqualToAnchor:titleLabel.trailingAnchor constant:20]
    ]];
    
    return rowView;
}

- (UIView *)createSwitchRowWithTitle:(NSString *)title subtitle:(NSString *)subtitle {
    UIView *rowView = [[UIView alloc] init];
    rowView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.0];
    rowView.layer.cornerRadius = 8;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:1.0];
    [rowView addSubview:titleLabel];
    
    UILabel *subtitleLabel = [[UILabel alloc] init];
    subtitleLabel.text = subtitle;
    subtitleLabel.font = [UIFont systemFontOfSize:12];
    subtitleLabel.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1.0];
    [rowView addSubview:subtitleLabel];
    
    UISwitch *switchControl = [[UISwitch alloc] init];
    switchControl.tag = 100;
    [switchControl addTarget:self action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    [rowView addSubview:switchControl];
    
    titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    switchControl.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [titleLabel.leadingAnchor constraintEqualToAnchor:rowView.leadingAnchor constant:16],
        [titleLabel.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor constant:-8],
        
        [subtitleLabel.leadingAnchor constraintEqualToAnchor:rowView.leadingAnchor constant:16],
        [subtitleLabel.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor constant:8],
        
        [switchControl.trailingAnchor constraintEqualToAnchor:rowView.trailingAnchor constant:-16],
        [switchControl.centerYAnchor constraintEqualToAnchor:rowView.centerYAnchor]
    ]];
    
    return rowView;
}

- (void)setupConstraints {
    self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
    self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
    self.birthdayCardView.translatesAutoresizingMaskIntoConstraints = NO;
    self.infoSectionView.translatesAutoresizingMaskIntoConstraints = NO;
    self.notificationSectionView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        [self.scrollView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
        [self.scrollView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
        [self.scrollView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
        [self.scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
        
        [self.contentView.topAnchor constraintEqualToAnchor:self.scrollView.topAnchor constant:20],
        [self.contentView.leadingAnchor constraintEqualToAnchor:self.scrollView.leadingAnchor constant:20],
        [self.contentView.trailingAnchor constraintEqualToAnchor:self.scrollView.trailingAnchor constant:-20],
        [self.contentView.bottomAnchor constraintEqualToAnchor:self.scrollView.bottomAnchor constant:-20],
        [self.contentView.widthAnchor constraintEqualToAnchor:self.scrollView.widthAnchor constant:-40],
        
        [self.birthdayCardView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor],
        [self.birthdayCardView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.birthdayCardView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
        [self.birthdayCardView.heightAnchor constraintEqualToConstant:200],
        
        [self.infoSectionView.topAnchor constraintEqualToAnchor:self.birthdayCardView.bottomAnchor constant:20],
        [self.infoSectionView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.infoSectionView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
        [self.infoSectionView.heightAnchor constraintEqualToConstant:250],
        
        [self.notificationSectionView.topAnchor constraintEqualToAnchor:self.infoSectionView.bottomAnchor constant:20],
        [self.notificationSectionView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.notificationSectionView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
        [self.notificationSectionView.heightAnchor constraintEqualToConstant:230]
    ]];
}

- (void)updateUI {
    self.nameLabel.text = self.birthday.name;
    self.lunarDateLabel.text = [NSString stringWithFormat:@"农历%@%@", [self.birthday lunarMonthName], [self.birthday lunarDayName]];
    
    NSInteger days = [self.birthday daysUntilBirthday];
    if (days == 0) {
        self.countdownLabel.text = @"今天就是生日！";
    } else {
        self.countdownLabel.text = [NSString stringWithFormat:@"距离生日还有 %ld 天", (long)days];
    }
    
    self.notifyOnDaySwitch.on = self.birthday.notifyOnDay;
    self.notifyOneDayBeforeSwitch.on = self.birthday.notifyOneDayBefore;
    self.notifyOneWeekBeforeSwitch.on = self.birthday.notifyOneWeekBefore;
}

- (NSString *)formatDate:(NSDate *)date {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月dd日";
    return [formatter stringFromDate:date];
}

- (void)backButtonTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)editButtonTapped {
    // 这里可以实现编辑功能，暂时显示提示
    [self showAlertWithTitle:@"提示" message:@"编辑功能开发中..."];
}

- (void)deleteButtonTapped {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认删除" 
                                                                   message:@"确定要删除这个生日提醒吗？" 
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" 
                                                           style:UIAlertActionStyleCancel 
                                                         handler:nil];
    [alert addAction:cancelAction];
    
    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" 
                                                           style:UIAlertActionStyleDestructive 
                                                         handler:^(UIAlertAction * _Nonnull action) {
        if (self.deleteBlock) {
            self.deleteBlock();
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alert addAction:deleteAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)switchValueChanged:(UISwitch *)sender {
    if (sender == self.notifyOnDaySwitch) {
        self.birthday.notifyOnDay = sender.isOn;
    } else if (sender == self.notifyOneDayBeforeSwitch) {
        self.birthday.notifyOneDayBefore = sender.isOn;
    } else if (sender == self.notifyOneWeekBeforeSwitch) {
        self.birthday.notifyOneWeekBefore = sender.isOn;
    }
    
    if (self.completionBlock) {
        self.completionBlock(self.birthday);
    }
}

- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title 
                                                                   message:message 
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" 
                                                       style:UIAlertActionStyleDefault 
                                                     handler:nil];
    [alert addAction:okAction];
    
    [self presentViewController:alert animated:YES completion:nil];
}

@end 